package com.yu1996.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yu1996.gulimall.product.dao.BrandDao;
import com.yu1996.gulimall.product.dao.CategoryDao;
import com.yu1996.gulimall.product.entity.BrandEntity;
import com.yu1996.gulimall.product.entity.CategoryEntity;
import com.yu1996.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu1996.common.utils.PageUtils;
import com.yu1996.common.utils.Query;

import com.yu1996.gulimall.product.dao.CategoryBrandRelationDao;
import com.yu1996.gulimall.product.entity.CategoryBrandRelationEntity;
import com.yu1996.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.util.StringUtils;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends
    ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements
    CategoryBrandRelationService {

  @Autowired
  private BrandDao brandDao;
  @Autowired
  private BrandService brandService;
  @Autowired
  private CategoryDao categoryDao;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {

    IPage<CategoryBrandRelationEntity> page = this.page(
        new Query<CategoryBrandRelationEntity>().getPage(params),
        new QueryWrapper<>()
    );
    return new PageUtils(page);
  }

  /**
   * 获取品牌关联的分类列表
   */
  @Override
  public List<CategoryBrandRelationEntity> listForBrandId(Long brandId) {
    QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
    if (!StringUtils.isEmpty(brandId)) {
      queryWrapper.eq("brand_id", brandId);
    }
    return this.list(queryWrapper);
  }

  /**
   * 新增品牌与分类关联关系 将分类名和品牌名一同查出来保存数据库
   */
  @Override
  public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
    CategoryEntity category = categoryDao.selectById(categoryBrandRelation.getCatelogId());
    BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
    if (category != null && brandEntity != null) {
      categoryBrandRelation.setBrandName(brandEntity.getName());
      categoryBrandRelation.setCatelogName(category.getName());
    }
    this.save(categoryBrandRelation);
  }

  /**
   * 更新品牌名
   */
  @Override
  public void updateBrand(Long brandId, String name) {
    CategoryBrandRelationEntity categoryBrandRelation = new CategoryBrandRelationEntity();
    categoryBrandRelation.setBrandId(brandId);
    categoryBrandRelation.setBrandName(name);
    this.update(categoryBrandRelation,
        new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
  }

  /**
   * 更新分类名
   */
  @Override
  public void updateCategory(Long catId, String name) {
    this.baseMapper.updateCategory(catId, name);
  }

  /**
   * 获取分类关联的品牌
   */
  @Override
  public List<BrandEntity> getBrandsByCatId(Long artId) {
    List<CategoryBrandRelationEntity> categoryBrandRelationEntities = this.baseMapper.selectList(
        new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", artId));
    List<BrandEntity> collect = categoryBrandRelationEntities.stream().map(entity -> {
      BrandEntity brandEntity = brandService.getById(entity.getBrandId());
      return brandEntity;
    }).collect(Collectors.toList());
    return collect;
  }

}